from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings
# serializer = Serializer(秘钥, 有效期秒)
serializer = Serializer(settings.SECRET_KEY, 300)
# serializer.dumps(数据), 返回bytes类型
token = serializer.dumps({'uname': 'zhangsan'})
token = token.decode()
# 检验token
# 验证失败，会抛出itsdangerous.BadData异常
serializer = Serializer(settings.SECRET_KEY, 300)

data = serializer.loads(token)

#openid签名处理(对openid进⾏加密)
# oauth/utils.py
def generate_secret_openid(openid):
 """
 签名openid
 :param openid: ⽤户的openid
 :return: access_token
 """
 # 创建序列化器对象给数据加密
 serializer = Serializer(settings.SECRET_KEY,expires_in=600)
 data = {'openid': openid}
 token = serializer.dumps(data)
 return token.decode()


